package Day20;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo02 {
    public static void main(String[] args) throws InterruptedException {
        //缓存线程池(长度无限制)
        //任务加入后的执行流程
        //1.判断线程池是否存在空闲线程
        //2.存在则使用，不存在则创建线程放入线程池然后使用
        ExecutorService service = Executors.newCachedThreadPool();//创建缓存线程的池
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "锄禾日当午");
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "锄禾日当午");
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "锄禾日当午");
            }
        });

        Thread.sleep(1000);
        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "锄禾日当午");
            }
        });
    }
}
